#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>

class Solution {
 public:
  std::vector<int> sortedSquares(std::vector<int> &nums)
  {
    std::vector<int> ret(nums.size(), 0);
    int k = nums.size() - 1;

    int i = 0, j = nums.size() - 1;
    while (i <= j)
    {
      if (abs(nums[i]) > abs(nums[j]))
      {
        ret[k--] = nums[i] * nums[i];
        ++i;
      } else
      {
        ret[k--] = nums[j] * nums[j];
        --j;
      }
    }
    return ret;
  }
};
